package cn.h8k.hj212.starter.example;

import cn.h8k.hj212.codec.Hj212EntityCodec;
import cn.h8k.hj212.config.Hj212Config;
import cn.h8k.hj212.entity.Hj212Message;
import cn.h8k.hj212.plugin.PluginManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Spring Boot Starter使用示例
 * 展示如何在Spring Boot应用中使用HJ212解析库
 * 
 * @author H8K Team
 * @since 1.0
 */
@SpringBootApplication
public class SpringBootStarterExample implements CommandLineRunner {

    @Autowired
    private Hj212EntityCodec hj212EntityCodec;

    @Autowired
    private Hj212Config hj212Config;

    @Autowired(required = false)
    private PluginManager pluginManager;

    public static void main(String[] args) {
        SpringApplication.run(SpringBootStarterExample.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("=== Spring Boot HJ212 Starter 示例 ===\n");

        // 1. 显示配置信息
        showConfiguration();

        // 2. 显示插件信息
        showPluginInfo();

        // 3. 演示编解码功能
        demonstrateCodec();

        System.out.println("\n=== 示例完成 ===");
    }

    /**
     * 显示配置信息
     */
    private void showConfiguration() {
        System.out.println("1. 配置信息");
        System.out.println("============");
        System.out.println("缓存启用: " + hj212Config.isEnableCache());
        System.out.println("验证启用: " + hj212Config.isEnableValidation());
        System.out.println("插件启用: " + hj212Config.isEnablePlugins());
        System.out.println("缓存大小: " + hj212Config.getCacheSize());
        System.out.println("默认版本: " + hj212Config.getDefaultVersion().getFullName());
        System.out.println("编解码器类型: " + hj212EntityCodec.getClass().getSimpleName());
        System.out.println();
    }

    /**
     * 显示插件信息
     */
    private void showPluginInfo() {
        System.out.println("2. 插件信息");
        System.out.println("============");
        if (pluginManager != null) {
            System.out.println("插件管理器: 已注入");
            System.out.println("已注册插件数量: " + pluginManager.getAllPlugins().size());
            pluginManager.getAllPlugins().forEach((name, plugin) -> {
                System.out.println("  - " + name + " v" + plugin.getVersion());
                System.out.println("    支持监测因子: " + plugin.getSupportedFactorCodes().size() + " 个");
            });
        } else {
            System.out.println("插件管理器: 未启用");
        }
        System.out.println();
    }

    /**
     * 演示编解码功能
     */
    private void demonstrateCodec() {
        System.out.println("3. 编解码演示");
        System.out.println("==============");

        String sampleData = "##0131QN=20240709120000123;ST=31;CN=2011;PW=123456;MN=V130427GDSJ001;Flag=4;CP=&&DataTime=20240709120000;avoc01-Rtd=0.021,avoc01-Flag=N&&7BD3\r\n";

        try {
            // 解码
            Hj212Message message = hj212EntityCodec.decode(sampleData);
            System.out.println("✓ 解码成功:");
            System.out.println("  设备标识: " + message.getMn());
            System.out.println("  系统编号: " + message.getSt());
            System.out.println("  命令编号: " + message.getCn());
            System.out.println("  请求编号: " + message.getQn());

            // 编码
            String encoded = hj212EntityCodec.encode(message);
            System.out.println("✓ 编码成功，长度: " + encoded.length() + " 字符");

            // 验证一致性
            boolean consistent = sampleData.equals(encoded);
            System.out.println("✓ 编解码一致性: " + (consistent ? "通过" : "失败"));

        } catch (Exception e) {
            System.err.println("✗ 编解码失败: " + e.getMessage());
        }
    }
}
